//
// Created by v on 2019/10/25 下午6:12.
// Copyright (c) 2019 jmdvirus All rights reserved.
//
//     ******          *             *--*
//       ||            *             |
//       ||  *--*--*---*  * *-********--*
//    * //   *  *  *   *\ / ||  |  |    |
//     **    *  *  *---* *  *   *--* *--*
//

#include "CMTimeout.h"

CMTimeout::CMTimeout()
{
    m_now        = 0;
    m_maxTimeout = 0;
    m_started    = false;
}

CMTimeout::CMTimeout(int maxTimeout)
{
    m_maxTimeout = maxTimeout;
    m_now        = 0;
}

int CMTimeout::start()
{
    if (m_started)
        return 0;
    m_started = true;
    m_now     = time(NULL);
    return 0;
}

int CMTimeout::check()
{
    if (!m_started)
        return 0;
    if (m_now == 0)
    {
        m_now = time(NULL);
    }
    if (time(NULL) - m_now > m_maxTimeout)
    {
        return 1;
    }
    return -1;
}

void CMTimeout::clear()
{
    m_now     = 0;
    m_started = false;
}